package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.SysPayCofDAO;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.BeanUtils;
import ctd.util.annotation.RpcService;
import hcn.base.feerule.SysPayCof;
import hcn.service.feerule.ISysPayCofService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import pcn.bean.operationPlatform.request.ExpressCostReq;
import pcn.bean.operationPlatform.response.ExpressCostResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xhf
 * @date 2019/6/18 20:25
 * @Description
 */
@SsdevService("sysPayCofService")
public class SysPayCofServiceImpl implements ISysPayCofService {
    @Autowired
    private SysPayCofDAO sysPayCofDAO;

    @Override
    @RpcService
    public void saveSysPayCof(SysPayCof sysPayCof) {
        if (CommonUtils.isNotNull(sysPayCof.getSysPayCofId())) {
            sysPayCof.setGmtModified(new Date());
            sysPayCofDAO.update(sysPayCof);

        } else {
            sysPayCof.setSysPayCofId(UUID.randomUUID().toString().replace("-", ""));
            List<SysPayCof> sysPayCofList = sysPayCofDAO.findSysPayCof(sysPayCof.getOrgId(),sysPayCof.getModuleId(),sysPayCof.getDeliveryModeCode());
            if(CommonUtils.isNotEmpty(sysPayCofList) && sysPayCofList.size()>1){
                for (SysPayCof sysPayCof1:sysPayCofList) {
                    sysPayCofDAO.remove(sysPayCof1.getSysPayCofId());
                }
            }
            sysPayCof.setGmtCreate(new Date());
            sysPayCof.setGmtModified(new Date());
            sysPayCofDAO.save(sysPayCof);
        }
    }


    @Override
    @RpcService
    public void deleteSysPayCof(SysPayCof sysPayCof) {
        sysPayCofDAO.remove(sysPayCof.getSysPayCofId());
    }

    @Override
    @RpcService
    public SysPayCof getPayModeCof(String hospitalCode, String moduleId, String deliveryModeCode) {
        List<SysPayCof> sysPayCofList = sysPayCofDAO.findSysPayCof(hospitalCode, moduleId, deliveryModeCode);
        if(CommonUtils.isNotEmpty(sysPayCofList)){
            SysPayCof payCof = sysPayCofList.get(0);
            return payCof;
        }
        return null;
    }


    @Override
    @RpcService
    public List<SysPayCof> findSysPayCofByModuleId(String orgId, String moduleId) {
       List<SysPayCof> sysPayCofs = sysPayCofDAO.findSysPayCofList(orgId,moduleId);
        return sysPayCofs;
    }

    /**
     * @Author:FangZhiyi
     * @Description 获取快递公司列表
     * @Params [queryFareReq]
     * @Return BaseResponse
     * @Data Created in 2020/10/16 21:03
     * @Moddified By:
     */
    @Override
    @RpcService
    public List<ExpressCostResp> getDeliveryModeCof(ExpressCostReq queryFareReq) throws ControllerException {
        List<ExpressCostResp> expressCostRespList = new ArrayList<>();
        List<SysPayCof> sysPayCofs = sysPayCofDAO.findSysPayCofList(queryFareReq.getOrgId(), queryFareReq.getModuleId());
        if (!CollectionUtils.isEmpty(sysPayCofs)) {
            for (SysPayCof sysPayCof : sysPayCofs) {
                ExpressCostResp expressCostResp = new ExpressCostResp();
                BeanUtils.copy(sysPayCof, expressCostResp);
                expressCostRespList.add(expressCostResp);
            }
        } else {
            throw new ControllerException("查询获取快递公司列表为空:");
        }
        return expressCostRespList;
    }

}
